﻿//产品信息js 
$(function () {    
    //将菜单栏标标移到点击处
    $(".nav-list li").removeClass("active");
    $("#info_li").addClass("active");
    //表头
    $('#dg').datagrid({
        url: "/ProductInfo/Op",
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        method: "get",
        selectOnCheck: false,
        checkOnSelect: false,
        singleSelect: true,
        toolbar: "#toolbar",
        columns: [[
            {
                field: 'ck', checkbox: true
            },
            {
                field: 'op', title: '操作', width: 120, align: 'center',
                formatter: function (value, row, index) {
                    if (row.IsPublished == 0) {
                        str = "";
                        str += '<button class="btn btn-inverse btn-xs" style="margin:3px;" onclick="producteditor(' + index + ')">编辑</button>';
                        str += '<button class="btn btn-danger btn-xs" style="margin:3px;" onclick="productdelete(this)">删除</button>';
                        str += '<button class="btn btn-primary btn-xs" style="margin:3px;width:65px"   onclick="noPublish(this)">发布</button>';
                        return str;
                    }
                    else {
                        str = "";
                        str += '<button class="btn btn-inverse btn-xs" style="margin:3px;" onclick="producteditor(' + index + ')">编辑</button>';
                        str += '<button class="btn btn-danger btn-xs" style="margin:3px;"  onclick="productdelete(this)">删除</button>';
                        str += '<button class="btn btn-success btn-xs" style="margin:3px;"  onclick="okPublish(this)">取消发布</button>';
                        return str;
                    }
                }
            },
            {
                field: 'ProductID', title: '产品编号', width: 60, align: 'center',
            },
            {
                field: 'IsPublished', title: '发布状态', width: 60, align: 'center',
                formatter: function (value, row, index) {
                    if (row.IsPublished == 0) {
                        return "<span class='red'>未发布</span>";
                    }
                    else {
                        return "<span class='green'>已发布</span>";
                    }
                }
            },

            {
                field: 'TypeName', title: '产品类别名称', width: 60, align: 'center',
            },
            {
                field: 'Title', title: '产品主题名称', width: 60, align: 'center',
            },
            {
                field: 'Model', title: '型号', width: 60, align: 'center',
            },
            {
                field: 'PublishDate', title: '发布时间', width: 60, align: 'center',
                formatter: function (value, row, index) {
                    if (row.IsPublished == 0) {
                        return "";
                    }
                    else {
                        return value;
                    }
                }
            },
            {
                field: 'CreateDate', title: '创建时间', width: 60, align: 'center',
            },
            {
                field: 'CreateUser', title: '创建人', width: 60, align: 'center',
            },
            {
                field: 'LastModifyDate', title: '修改时间', width: 60, align: 'center',
            },
            {
                field: 'LastModifyUser', title: '修改人', width: 60, align: 'center',
            },
            {
                field: 'ClickRate', title: '点击次数', width: 60, align: 'center',
                formatter: function (value, row, index) {
                    return value;
                }
            },

        ]]
    });
    var pager = $('#dg').datagrid().datagrid('getPager');
})
//产品删除方法
function productdelete() {
    $.messager.confirm('提示', '是否删除', function (r) {
        if (r) {
            var row = $('#dg').datagrid('getSelected');
            var param = {};
            param.ProductID = row.ProductID;
            $.ajax({
                type: "get",
                url: "/ProductInfo/Delete",
                async: true,
                data: param,
                dataType: "json",
                error: function (ajaxExceptionModel) {
                    //显示错误
                    var obj = JSON.stringify(ajaxExceptionModel.responseText);
                    alert(obj.messager);
                },
                success: function (result) {
                    $('#dg').datagrid('reload');
                    $.messager.alert('提示', '删除成功!');
                }
            });
        }
    })
}

//点击修改获取修改方法
function producteditor(index) {    
    var row = $('#dg').datagrid("getRows")[index];   
    location.href = '/ProductInfo/EditProdutInfo?productID='+ row.ProductID; 
}

//点击添加获取方法
function addInfo() {      
    location.href = '/ProductInfo/AddProductInfo'; 
}

//产品发布方法
function noPublish() {
    $.messager.confirm('提示', '是否发布', function (r) {
        if (r) {
            var row = $('#dg').datagrid('getSelected');
            var param = {};
            param.ProductID = row.ProductID;
            $.ajax({
                type: "get",
                url: "/ProductInfo/InfoPublish",
                async: true,
                data: param,
                dataType: "json",
                error: function (ajaxExceptionModel) {
                    //显示错误
                    var obj = JSON.stringify(ajaxExceptionModel.responseText);
                    alert(obj.messager);
                },
                success: function (result) {
                    $('#dg').datagrid('reload');
                    $.messager.alert('提示', '发布成功!');
                }
            })
        }
    })
}

//产品取消发布方法
function okPublish() {
    $.messager.confirm('提示', '是否取消发布', function (r) {
        if (r) {
            var row = $('#dg').datagrid('getSelected');
            var param = {};
            param.ProductID = row.ProductID;
            $.ajax({
                type: "get",
                url: "/ProductInfo/InfookPublish",
                async: true,
                data: param,
                dataType: "json",
                error: function (ajaxExceptionModel) {
                    //显示错误
                    var obj = JSON.stringify(ajaxExceptionModel.responseText);
                    alert(obj.messager);
                },
                success: function (result) {
                    $('#dg').datagrid('reload');
                    $.messager.alert('提示', '取消发布成功!');
                }
            })
        }
    })
}

//查询方法
function SearchAll(_pageNumber) {
    var _pageSize = $("#dg").datagrid("getPager").pagination("options").pageSize;
    var IsPublished = $('#isPublishedSearch').val();
    var ProductTypeID = $('#ProductTypeIDSearch').val();
    var beginPublishDate1 = $('#beginTime1').val();
    var endPublishDate1 = $('#endTime1').val();
    var beginPublishDate2 = $('#beginTime2').val();
    var endPublishDate2 = $('#endTime2').val();
    var param = {};
    param.pageSize = _pageSize;
    param.pageNumber = _pageNumber;
    if (beginPublishDate1 == "") {
        beginPublishDate1 = "0";
        endPublishDate1 = "0";
    }
    else if (beginPublishDate1 !== null) {
        if (endPublishDate1 == "") {
            var date1 = new Date();
            var year = date1.getFullYear();
            var month = date1.getMonth() * 1 + 1;
            if (month < 10) {
                month = "0" + month;
            }
            var day = date1.getDate();
            if (day < 10) {
                day = "0" + day;
            }
            var date2 = year + "-" + month + "-" + day;
            endPublishDate1 = date2;
        }
    }
    var flag = true;
    if (flag == true) {
        if (beginPublishDate2 == "") {
            beginPublishDate2 = "0";
            endPublishDate2 = "0";
        }
        else if (beginPublishDate2 !== null) {
            if (endPublishDate2 == "") {
                var date1 = new Date();
                var year = date1.getFullYear();
                var month = date1.getMonth() * 1 + 1;
                if (month < 10) {
                    month = "0" + month;
                }
                var day = date1.getDate();
                if (day < 10) {
                    day = "0" + day;
                }
                var date2 = year + "-" + month + "-" + day;
                endPublishDate2 = date2;
            }
        }
    }
    param.IsPublished = IsPublished;
    param.ProductTypeID = ProductTypeID;
    param.beginPublishDate1 = beginPublishDate1;
    param.endPublishDate1 = endPublishDate1;
    param.beginPublishDate2 = beginPublishDate2;
    param.endPublishDate2 = endPublishDate2;

    $.ajax({
        type: "get",
        url: "/ProductInfo/SearchAll",
        async: true,
        data: param,
        dataType: "json",
        error: function (ajaxExceptionModel) {
            //显示错误
            var obj = JSON.stringify(ajaxExceptionModel.responseText);
            alert(obj.messager);
        },
        success: function (result) {
            $('#dg').datagrid('loadData', result);
            $("#dg").datagrid("getPager").pagination({
                onSelectPage: function (pageNumber, pageSize) {
                    SearchAll(pageNumber);

                },
                onRefresh: function (pageNumber, pageSize) {
                    SearchAll(pageNumber);
                }
            });
        }
    })

}

//展示全部
function SearchAllall() {
    $('#dg').datagrid('reload');
}

//批量删除
function Bigde() {
    var ids = [];
    var rows = $('#dg').datagrid('getChecked');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].ProductID);
    }
    if (rows.length == 0) {
        $.messager.alert('提示', '请至少选择一行数据');
        return false;
    }
    if (rows) {
        $.messager.confirm('提示', '是否删除所选条目?', function (r) {
            if (r) {
                var param = {};
                param.param = JSON.stringify(ids);
                $.ajax({
                    type: "get",
                    url: "/ProductInfo/Bigde",
                    async: true,
                    data: param,
                    dataType: "json",
                    error: function (ajaxExceptionModel) {
                        //显示错误
                        var obj = JSON.stringify(ajaxExceptionModel.responseText);
                        alert(obj.ErrorMessage);
                    },
                    success: function (result) {
                        $.messager.alert('提示', '删除成功');
                        $('#dg').datagrid('reload');
                    }
                });
            }
        });
    }
}

//批量发布
function PublishAll() {
    var ids = [];
    var rows = $('#dg').datagrid('getChecked');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].ProductID);
    }

    if (rows.length == 0) {
        $.messager.alert('提示', '请至少选择一行数据');
        return false;
    }
    if (rows) {
        $.messager.confirm('提示', '是否发布所选条目?', function (r) {
            if (r) {
                var param = {};
                param.param = JSON.stringify(ids);
                $.ajax({
                    type: "get",
                    url: "/ProductInfo/IsPublishedAll",
                    async: true,
                    data: param,
                    dataType: "json",
                    error: function (ajaxExceptionModel) {
                        //显示错误
                        var obj = JSON.stringify(ajaxExceptionModel.responseText);
                        alert(obj.ErrorMessage);
                    },
                    success: function (result) {
                        $.messager.alert('提示', '发布成功');
                        $('#dg').datagrid('reload');
                    }
                });
            }
        });
    }
}
//不清楚
$.fn.datepicker.dates['cn'] = {
    days: ["周日", "周一", "周二", "周三", "周四", "周五", "周六", "周日"],
    daysShort: ["日", "一", "二", "三", "四", "五", "六", "七"],
    daysMin: ["日", "一", "二", "三", "四", "五", "六", "七"],
    months: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
    monthsShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
    today: "今天",
    clear: "清除"
};
$(function () {
    $(".timesearch").datepicker({
        format: "yyyy-mm-dd",
        language: "cn"
    });
})



//查找之后分页
function page(_pageNumber, _pageSize) {
    var p = {};
    p.pageNumber = _pageNumber;
    p.pageSize = _pageSize;
    p.selecttext = $("#selecttext").val();
    p.state = $("#state").val();
    $.ajax({
        type: 'get',
        url: '/ProductInfo/Op',
        async: true,
        dataType: 'json',
        data: p,
        success: function (data) {
            $("#dg").datagrid("loadData", data);
            $("#dg").datagrid("getPager").pagination({
                onSelectPage: function (pageNumber, pageSize) {
                    page(pageNumber, pageSize);

                },
                onRefresh: function (pageNumber, pageSize) {
                    page(pageNumber, pageSize);
                }
            });
        }
    });
}



